package com.hns.can.energy.web;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.service.CommonInitParamService;
import com.hns.can.common.web.BaseController;
import com.hns.can.energy.service.CarEnergyService;
import com.hns.can.energy.vo.NewEnergy;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.export.excel.excelNoTemplate.ExportExcelNoTempEntity;
import com.hns.tool.export.pdf.ExportPdfEntity;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;

/**
 * 车辆能耗控制器
 * @author:Fisher
 * @email:zhuangcaijin@126.com
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/carEnergy")
public class CarEnergyController extends BaseController{
    @Autowired
    CarEnergyService carEnergyService;
    @Autowired
    private CommonInitParamService commonInitParamService;
    
    final private String[] headerNames = new String[]{"能耗排名","所属机构","营运线路","车牌号","车辆编号","车辆类型","车辆型号",
        "燃料类型",//"电机型号",
        "运营日期","运行时长","总里程","运行里程","运行能耗","百公里能耗","标准能耗","能耗百分比","线路平均能耗","是否节能"};
    final private String[] headerFields = new String[]{"allrank", "organName", "lineName", "licPltNo", 
        "carInCd", "carType", "carModel","fuelTypeName", 
        //"engiNo", 
        "days", "rngOprtDuraTr","totalMile", "rngMile", "rngEnerCons",
        "enerConsMile","refrEnerCons","enerConsPers", "lineEnerConsMile", "isSaveEner"};

    
    /**
     * 车辆能耗查询
     * @param vo
     * @param response
     * @param request
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/searchCarEnergy")
    @ResponseBody
    public Object searchCarEnergy(NewEnergy vo) throws Exception {
        if (vo.getOrgIds() == null || vo.getBeginTime() == null || vo.getEndTime() == null) {
            return null;
        }
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        vo.setDriverRelTableName("T_COMM_ENER_CONS_STAT_INFO");
        if(PubUtil.isNotEmpty(vo.getEnergyType())){
        	commonInitParamService.dateType2DateTime(vo, vo.getDateType());
        }
        Pagination page = carEnergyService.searchCarEnergy(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
    
    /**
     * 车辆能耗导出excel                
     * @param vo
     * @param response
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/exportCarEnergyExcel")
    public void exportCarEnergyExcel(NewEnergy vo, HttpServletResponse response) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        List list = carEnergyService.exportCarEnergy(vo);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        ExportExcelNoTempEntity exportEntity = new ExportExcelNoTempEntity(
            Arrays.asList(headerNames), Arrays.asList(headerFields), list, URLEncoder.encode("车辆能耗明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"));
        ExportUtil.generatorExcelNoTemplate(exportEntity, response);
    }
    
    /**
     * 车辆能耗导出PDF
     * @param vo
     * @param response
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/exportCarEnergyPdf")
    public void exportCarEnergyPdf(NewEnergy vo, HttpServletResponse response) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        List list = carEnergyService.exportCarEnergy(vo);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        ExportPdfEntity pdfEntity = new ExportPdfEntity(
            Arrays.asList(headerNames), Arrays.asList(headerFields), list, URLEncoder.encode("车辆能耗明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"));
        
        ExportUtil.generatorPDF(pdfEntity, response);
    }
    
    
    /**
     * 车辆能耗查询
     * @param vo
     * @param response
     * @param request
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/getEnergyDetail")
    @ResponseBody
    public Object searchEnergyDetail(NewEnergy vo) throws Exception {
    	commonInitParamService.dateType2DateTime(vo, vo.getDateType());
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        vo.setDriverRelTableName("T_COMM_ENER_CONS_STAT_INFO");
        
        Pagination page = carEnergyService.searchCarEnergy(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
}
